﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraBars.Ribbon;
using CDMS.Common;
using CDMS.Entities;
using CDMS.BLL;

namespace CDMS.Forms
{
    public partial class frmCategoryInfo : XtraForm
    {
        Category_BLL category_BLL;
        EnumCategory enumCategory;
        Category category;
        int category_ID;

        public frmCategoryInfo(EnumCategory enumCategory, int category_ID)
        {
            InitializeComponent();
            this.layoutControl1.AllowCustomizationMenu = false;

            this.DialogResult = DialogResult.Cancel;
            category_BLL = new Category_BLL(enumCategory);
            this.enumCategory = enumCategory;
            this.category_ID = category_ID;
            if (category_ID > 0)
            {
                category = category_BLL.GetOne(category_ID);
                if (category != null)
                {
                    txtName.Text = category.Category_Name;
                    txtNote.Text = category.Category_Note;
                }
            }
            SetTitle(enumCategory, category_ID);
        }

        private void SetTitle(EnumCategory enumCategory, int iD)
        {
            string str = Globals.GetDescriptionFromEnumValue(enumCategory);

            lbTitle.Text = iD == 0 ? "Thêm " : "Sửa ";
            lbTitle.Text += str.ToLower();
            this.Text = lbTitle.Text;

            lblName.Text = "Tên " + str.ToLower();
            lblNote.Text = "Ghi chú";
        }

        bool CheckValid()
        {
            dxErrorProvider1.ClearErrors();
            

            if(string.IsNullOrEmpty(txtName.Text.Trim()))
            {
                ErrorProvider_SetError(txtName, lblName.Text + " không được để trống!.");
                return false;
            }

            //return dxValidationProvider1.Validate();
            return true;
        }
        void ErrorProvider_SetError(Control control, string errorText)
        {
            dxErrorProvider1.SetError(control, errorText);
            dxErrorProvider1.SetIconAlignment(control, ErrorIconAlignment.TopRight);
            //dxErrorProvider1.GetIconAlignment()
        }
            
        private void btnSave_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (!CheckValid())
                return;

            FormGlobals.ShowWaitForm();

            category = new Category();
            category.Category_ID = category_ID;
            category.Category_Name = txtName.Text;
            category.Category_Note = txtNote.Text;

            if (category_BLL.Exists(category))
            {
                ErrorProvider_SetError(txtName, lblName.Text + " đã tồn tại.");
                
                FormGlobals.Message_Warning(lblName.Text + " đã tồn tại.", "Cảnh báo");
                return;
            }

            if (category_ID == 0)
            {
                category_ID = category_BLL.Insert(category);
                if (category_ID > 0)
                {
                    FormGlobals.Message_Information("Thêm thành công", "Thông báo");
                    this.DialogResult = DialogResult.OK;
                }
                else
                    this.DialogResult = DialogResult.Cancel;
            }
            else
            {
                if (category_BLL.Update(category))
                {
                    FormGlobals.Message_Information("Sửa thành công", "Thông báo");
                    this.DialogResult = DialogResult.OK;
                }
                else
                    this.DialogResult = DialogResult.Cancel;
            }
            FormGlobals.CloseWaitForm();
        }


    }
}